<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/2/21
 * Time: 13:44
 */

namespace app\admin\controller;

use app\common\model\Ad as AdModel;
use app\admin\validate\AdValidate;
use app\common\model\Activity;

class Ad extends CheckLogin
{

    public function index()
    {
        if ($this->request->isAjax()) {
            $page = (int)$this->request->post('pageIndex');
            $limit = (int)$this->request->post('pageSize');
            $activityId = $this->request->post('dataActivityId', 0);
            $name = $this->request->post('dataName', '');
            $offset = ($page - 1) * $limit;
            $where = [];
            empty($name) or $where['title'] = ['like', '%'. $name .'%'];
            empty($activityId) or $where['activity'] = $activityId;
            $adModel = new AdModel();
            $totalCount = $adModel->where($where)->count();
            $list = $totalCount > 0 ? $adModel->allAd($where, $offset, $limit) : [];

            $data = [
                'code' => 0,
                'msg' => 'success',
                'list' => $list,
                'count' => $totalCount
            ];

            return $this->json($data);
        } else {
            $this->assign('activity', Activity::all());
            return $this->fetch();
        }
    }

    public function add()
    {
        if($this->request->isPost()) {
            $data = $this->request->post();
            $adValidate = new AdValidate();
            if($adValidate->scene('add')->check($data)){
                $ad = new AdModel();
                $data['start_time'] = strtotime($data['start_time']);
                $data['end_time'] = strtotime($data['end_time']);
                unset($data['file']);
                if($ad->add($data) != null){
                    return $this->jsonSuccess('添加成功', ['url'=>'/ad/index']);
                }
                $errMsg = '添加失败';
            }else{
                $errMsg = $adValidate->getError();
            }
            return $this->jsonFail($errMsg, ['url'=>'/ad/index']);
        } else {
            $this->assign('activity', Activity::all(['end_time' => ['gt', time()]]));
            return $this->fetch();
        }
    }

    public function edit()
    {
        if($this->request->isPost()) {
            $data = $this->request->post();
            $adValidate = new AdValidate();
            if($adValidate->scene('edit')->check($data)){
                $ad = new AdModel();
                $data['start_time'] = strtotime($data['start_time']);
                $data['end_time'] = strtotime($data['end_time']);
                unset($data['file']);
                if($ad->edit($data) != null){
                    return $this->jsonSuccess('修改成功', ['url'=>'/ad/index']);
                }
                $errMsg = '修改失败';
            }else{
                $errMsg = $adValidate->getError();
            }
            return $this->jsonFail($errMsg, ['url'=>'/ad/index']);
        } else {
            $id = $this->request->param('id');
            $row = AdModel::get($id);
            $this->assign('row', $row);
            $this->assign('activity', Activity::where(['end_time' => ['gt', time()]])->whereOr(['id' => $row['activity_id']])->select());
            return $this->fetch();
        }
    }
}
